package com.ssm.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.sfzy.entity.Charge;
import com.ssm.sfzy.entity.Teacher;
import com.ssm.sfzy.mapper.ChargeMapper;
import com.ssm.sfzy.mapper.TeacherMapper;
import com.ssm.sfzy.service.ChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fuyanru
 * @date 2024年03月30日 16:56
 */
@Service
public class ChargeServiceImpl implements ChargeService {
    @Autowired
    private ChargeMapper chargeMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void add(Charge entity) {
        Teacher teacher = teacherMapper.selectByName(entity.getName());
        entity.setTeacherId(teacher.getId());
        chargeMapper.insert(entity);
    }

    @Override
    public void edit(Charge entity) {
        Teacher teacher = teacherMapper.selectByName(entity.getName());
        entity.setTeacherId(teacher.getId());
          chargeMapper.update(entity);
    }

    @Override
    public void remove(Integer id) {
        chargeMapper.delete(id);
    }

    @Override
    public void removeBatch(Integer[] ids) {
        chargeMapper.deleteBatch(ids);
    }

    @Override
    public List<Charge> find(Charge entity) {
        return chargeMapper.select(entity);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, Charge entity) {
        PageHelper.startPage(pageNum,pageSize);
        List<Charge> list = chargeMapper.select(entity);
        //
        PageInfo<Charge> pageInfo = new PageInfo<>(list);
        // 整合处理
        Map<String,Object> map = new HashMap<>();
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    @Override
    public Charge findById(Integer id) {
        return  chargeMapper.selectById(id);
    }
}
